﻿Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Data
Imports System.Collections
Imports System.Windows.Forms
Imports DataObject
Imports System.Data.SqlClient

Public Class SqlServerLoaiTien

    Private oqLBase As New hDBase
    Private oDataSet As New DataSet
    Private oDatatable As New DataTable

    Public Function GetAllLoaiTien(ByVal sConnString As String) As DataTable
        oqLBase.Connection(sConnString)
        oDataSet = oqLBase.GetData("Get_All_LoaiTiens", "LoaiTien")
        Return oDataSet.Tables("LoaiTien")
    End Function

    Public Function InsertLoaiTien(ByVal sConnString As String, ByVal oLoaiTien As LoaiTien) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoLoaiTien", SqlDbType.Int), _
                                           New SqlParameter("@MaLoaiTien", SqlDbType.VarChar, 20), _
                                           New SqlParameter("@TenLoaiTien", SqlDbType.NVarChar, 50), _
                                           New SqlParameter("@ToanTuQuyDoi", SqlDbType.NVarChar, 5), _
                                           New SqlParameter("@TyGiaQuyDoi", SqlDbType.Money)}
        SqlParam(0).Direction = ParameterDirection.Output
        SqlParam(1).Value = oLoaiTien.MaLoaiTien
        SqlParam(2).Value = oLoaiTien.TenLoaiTien
        SqlParam(3).Value = oLoaiTien.ToanTuQuyDoi
        SqlParam(4).Value = oLoaiTien.TyGiaQuyDoi
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Insert_LoaiTien", "LoaiTien", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function UpdateLoaiTien(ByVal sConnString As String, ByVal oLoaiTien As LoaiTien) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoLoaiTien", SqlDbType.Int), _
                                           New SqlParameter("@MaLoaiTien", SqlDbType.VarChar, 20), _
                                           New SqlParameter("@TenLoaiTien", SqlDbType.NVarChar, 50), _
                                           New SqlParameter("@ToanTuQuyDoi", SqlDbType.NVarChar, 5), _
                                           New SqlParameter("@TyGiaQuyDoi", SqlDbType.Money)}
        SqlParam(0).Value = oLoaiTien.SoLoaiTien
        SqlParam(1).Value = oLoaiTien.MaLoaiTien
        SqlParam(2).Value = oLoaiTien.TenLoaiTien
        SqlParam(3).Value = oLoaiTien.ToanTuQuyDoi
        SqlParam(4).Value = oLoaiTien.TyGiaQuyDoi
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Update_LoaiTien", "LoaiTien", SqlParam)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End Try
    End Function

    Public Function DeleteLoaiTien(ByVal sConnString As String, ByVal SoLoaiTien As Integer) As Boolean
        Dim SqlParam As SqlParameter() = {New SqlParameter("@SoLoaiTien", SqlDbType.Int)}
        SqlParam(0).Value = SoLoaiTien
        Try
            oqLBase.Connection(sConnString)
            oqLBase.RunStoreProcedures("Delete_LoaiTien", "LoaiTien", SqlParam)
            Return True
        Catch ex As SqlException
            Select Case ex.ErrorCode
                Case -2146232060
                    MessageBox.Show("Không thể xóa dữ liệu này. Loại tiền tệ này đang được sử dụng trong phần nghiệp vụ khác", "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return False
            End Select
        End Try
    End Function

End Class
